package com.hkts.naturalgas.datahandler.sheldertask;

import com.hkts.naturalgas.association.service.IHktsRequestAddressAssociationService;
import com.hkts.naturalgas.datahandler.constance.EnviromentConstance;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionDictionariesService;
import com.hkts.naturalgas.dictionaries.utils.DBUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassNameDataEliminationShelder
 * @Description
 * @Author yck
 * @Date2021-10-21 9:41
 * @Version V1.0
 **/
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Lazy(false)
@Slf4j
public class DataEliminationShelder {
    /**
     * 功能描述:
     * 数据删除策略
     *
     * @return: void
     * @Author: 杨春科
     * @Date: 2021-10-21 9:43
     * @Description 24小时之前的数据10秒留一条数据 一个月之前的数据一分钟留一条，半年之前的数据10分钟一条，一年之前的数据全部清除
     * 10分钟执行一次
     */
    @Autowired
    public DBUtils dBUtils;
    @Autowired
    public IHktsEnvironmentProtectionDictionariesService iHktsEnvironmentProtectionDictionariesService;

    @Autowired
    public IHktsRequestAddressAssociationService iHktsRequestAddressAssociationService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
   @Async
   //@Scheduled(cron = "0 0 0/1 * * ?")
    public void productData() throws Exception {
       CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
       log.info("---------数据淘汰开始----------");
       List<String> sqlList = new ArrayList<String>();
       Long startTime = System.currentTimeMillis();
       HktsEnvironmentProtectionDictionaries hktsEnvironmentProtectionDictionaries = new HktsEnvironmentProtectionDictionaries();
       List<HktsEnvironmentProtectionDictionaries> list = iHktsEnvironmentProtectionDictionariesService.selectList(hktsEnvironmentProtectionDictionaries);
       LocalDateTime now = LocalDateTime.now();
       LocalDateTime day = now.plusDays(-1);
       LocalDateTime month = now.plusMonths(-1);
       LocalDateTime halfYear = now.plusMonths(-6);
       LocalDateTime year = now.plusMonths(-36);
       DateTimeFormatter formatter
               = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

       String format = formatter.format(day);
       System.out.println(format);
       List<String> listDate = new ArrayList<String>();
       listDate.add(formatter.format(day));
       listDate.add(formatter.format(month));
       listDate.add(formatter.format(halfYear));
       listDate.add(formatter.format(year));
       for (int i = 0; i < list.size(); i++) {
           //判断是数据库远程删除其他数据库数据
           if (!list.get(i).getIsHttp().equals(EnviromentConstance.IS_HTTP)) {
               HktsEnvironmentProtectionDictionaries hktsEnvironmentProtectionDictionaries1 = list.get(i);
               //淘汰车间数据
               iHktsRequestAddressAssociationService.deleteData(hktsEnvironmentProtectionDictionaries1, listDate);
           }
           sqlList.addAll(dBUtils.deleteSql(list.get(i).getTableName(), listDate));
       }
       dBUtils.updateBatch(sqlList);
       Long endTime = System.currentTimeMillis();
       Long tempTime = (endTime - startTime);
       System.out.println("数据淘汰花费时间：" +
               (((tempTime / 86400000) > 0) ? ((tempTime / 86400000) + "d") : "") +
               ((((tempTime / 86400000) > 0) || ((tempTime % 86400000 / 3600000) > 0)) ? ((tempTime % 86400000 / 3600000) + "h") : ("")) +
               ((((tempTime / 3600000) > 0) || ((tempTime % 3600000 / 60000) > 0)) ? ((tempTime % 3600000 / 60000) + "m") : ("")) +
               ((((tempTime / 60000) > 0) || ((tempTime % 60000 / 1000) > 0)) ? ((tempTime % 60000 / 1000) + "s") : ("")) +
               ((tempTime % 1000) + "ms"));
       log.info("---------数据淘汰结束---------");
       }, threadPoolExecutor);
   }

}
